#include "EnemyBank.h"
#include <string>
#include "GameConfig.h"
#include "Config.h"
#include "pugixml.hpp"

using namespace std;
using namespace pugi;

EnemyBank::EnemyBank(void)
{
	_listEnemyFactory = NULL;
}


EnemyBank::~EnemyBank(void)
{
	CC_SAFE_RELEASE(_listEnemyFactory);
}

EnemyBank* EnemyBank::sharedEnemyBank()
{
	if (!_instance)
		_instance = new EnemyBank();
	return _instance;
}

void EnemyBank::clearBank()
{
	delete _instance;
	_instance = NULL;
}

void EnemyBank::initBank()
{
	setEnemyFactories(CCDictionary::create());

	// Do anything
	string data = string(dynamic_cast<CCString*>(GameConfig::sharedGameConfig()->getConfigData()->objectForKey(CONFIG_ENEMY))->getCString());

	xml_document doc;
	xml_parse_result result = doc.load(data.c_str());
	CCAssert(result.status == status_ok, "FAILD");

	xml_node node = doc.child("Root").child("Enemies");
	for (xml_node i = node.child("Enemy"); i; i = i.next_sibling("Enemy"))
	{
		EnemyFactory* eFactory = EnemyFactory::create();
		eFactory->parseXml(i);
		_listEnemyFactory->setObject(eFactory, eFactory->getId()->getCString());
	}
}

EnemyFactory* EnemyBank::getEnemyFactory( char* type )
{
	return dynamic_cast<EnemyFactory*>(_listEnemyFactory->objectForKey(type));
}

EnemyBank* EnemyBank::_instance = NULL;
